using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;

namespace ebookreader.Utils
{
    public class Project
    {

        public static void getEBook(string id, Action<string> callback, Action error)
        {
            try{
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.gutenberg.org/ebooks/"+id+".rdf");
            //req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            req.Method = "GET";
            req.BeginGetResponse(
                (asyncResult) =>
                {
                    try{
                    HttpWebResponse objResponse = null;
                    StreamReader objs = null;
                    
                    HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;

                    objResponse = (HttpWebResponse)request.EndGetResponse(asyncResult);
                    objs = new StreamReader(objResponse.GetResponseStream());

                    XmlSerializer ser = new XmlSerializer(typeof(RDF));
                    var desc = ser.Deserialize(objs) as RDF;

                        for (int i = 0; i < desc.file.Length; i++)
                        {
                            var link = desc.file[i].about;
                            if (link.EndsWith(".txt.utf8") || link.EndsWith(".txt"))
                            {
                                HttpWebRequest requ = (HttpWebRequest)HttpWebRequest.Create(link);
                                requ.Method = "GET";
                                requ.BeginGetResponse(
                                    (asyncResult2) =>
                                    {
                                        HttpWebRequest request2 = (HttpWebRequest)asyncResult2.AsyncState;

                                        var objResponse2 = (HttpWebResponse)request2.EndGetResponse(asyncResult2);
                                        var stream = objResponse2.GetResponseStream();
                                        byte [] buffer= new byte[stream.Length];
                                        stream.Read(buffer,0,(int)stream.Length);

                                        var enc = Encoding.UTF8;

                                        try
                                        {
                                            var ct = objResponse2.Headers["Content-Type"];
                                            var charset = ct.Split(',').First(st => st.Contains("charset")).Split('=')[1];
                                            enc = Encoding.GetEncoding(charset);
                                        }
                                        catch (Exception) { }
                                        var s = enc.GetString(buffer, 0, (int)stream.Length);
                                        callback(s);
                                    }
                                    , requ);
                                return;
                            }                            
                        }
                    }
                    catch (Exception e)
                    {
                        error();
                    }
                }
                , req);
            }
            catch(Exception)
            {
                error();
            }
        }



        public static void getCustomEBook(string url, Action<string> callback, Action error)
        {
            try
            {
               
                HttpWebRequest requ = (HttpWebRequest)HttpWebRequest.Create(url);
                requ.Method = "GET";
                requ.BeginGetResponse(
                    (asyncResult2) =>
                    {
                        try
                        {
                            HttpWebRequest request2 = (HttpWebRequest)asyncResult2.AsyncState;

                            var objResponse2 = (HttpWebResponse)request2.EndGetResponse(asyncResult2);
                            var stream = objResponse2.GetResponseStream();
                            byte[] buffer = new byte[stream.Length];
                            stream.Read(buffer, 0, (int)stream.Length);

                            var enc = Encoding.UTF8;
                                                        
                            try
                            {
                                var ct = objResponse2.Headers["Content-Type"];
                                var charset = ct.Split(',').First(st => st.Contains("charset")).Split('=')[1];
                                enc = Encoding.GetEncoding(charset);
                            }
                            catch (Exception) { }
                            var s = enc.GetString(buffer, 0, (int)stream.Length);
                            callback(s);
                        }
                        catch (Exception e)
                        {
                            error();
                        }
                    }
                    , requ);
                                   
                
            }
            catch (Exception)
            {
                error();
            }
        }




       public static void GetRequestStreamCallbackForBook(IAsyncResult asyncResult)
        {
            IAsyncResult ar = null;

            int i = 0;
        }


        protected static void GetScrapingResponse(IAsyncResult result)
        {
            int i = 1;
        }



    }
}
